<?php




/*
CREATE FUNCTION order_total(order_id INT) RETURNS INT 
BEGIN
	DECLARE Temp INT;
	SELECT SUM(price) INTO Temp FROM order_items WHERE orders_id=order_id;

	RETURN IFNULL(Temp, 0);
END
*/

/*
CREATE VIEW order_totals AS SELECT orders.id, sum(qty*price) AS total FROM `orders`, `order_items` WHERE orders.id=order_items.orders_id GROUP BY orders.id
*/

require_once(DIR_LIB."VsCore".DIR_SEP."db.php");
require_once(DIR_LIB."VsCore".DIR_SEP."vsauth.php");

require_once(DIR_LIB."VsShop".DIR_SEP."vsorder.php");

class VsOrderFactory {

	
	static function makeLastOrder() {
		$q = "SELECT MAX(id) AS last_id FROM `orders`";

		$db = VsDatabase::Instance();
		$res = $db->query($q);
		$rOrder = mysql_fetch_object($res)) 
		
		return new VsOrder($rOrder->last_id);
	}
	//============================================================================================================================


	static function countOrdersForToday() {
		$q = "SELECT orders.id, orders.status, order_totals.total FROM `orders`, `order_totals` WHERE DAY(CURDATE())=DAY(orders.date) AND order_totals.orders_id=orders.id";

		$orders = array('processed'=>0, 'failed'=>0, 'queued'=>0, 'total'=>0);
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($rOrder = mysql_fetch_object($res)) {
			$orders['total'] += $rOrder->total;

			if($rOrder->status=2)	// Processed
				$orders['processed']++;
			elseif(($rOrder->status=3) || ($rOrder->status=3))	// Failed Or Declined
				$orders['failed']++;
			elseif($rOrder->status=1)	// Queued
				$orders['queued']++;
		}

		return $orders;
	}
	//=========================================================================================================================

	static function countOrdersForThisMonth() {
		$q = "SELECT orders.id, orders.status, order_totals.total FROM `orders`, `order_totals` WHERE MONTH(CURDATE())=MONTH(orders.date) AND order_totals.orders_id=orders.id";

		$orders = array('processed'=>0, 'failed'=>0, 'queued'=>0, 'total'=>0);
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($rOrder = mysql_fetch_object($res)) {
			$orders['total'] += $rOrder->total;

			if($rOrder->status=2)	// Processed
				$orders['processed']++;
			elseif(($rOrder->status=3) || ($rOrder->status=3))	// Failed Or Declined
				$orders['failed']++;
			elseif($rOrder->status=1)	// Queued
				$orders['queued']++;
		}

		return $orders;
	}
	//==========================================================================================================================================

	static function countOrdersForThisWeek() {
		$q = "SELECT orders.id, orders.status, order_totals.total FROM `orders`, `order_totals` WHERE DATE_SUB(CURDATE(),INTERVAL DAYOFWEEK(CURDATE()) DAY) <= orders.date AND order_totals.orders_id=orders.id";


		$orders = array('processed'=>0, 'failed'=>0, 'queued'=>0, 'total'=>0);
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($rOrder = mysql_fetch_object($res)) {
			$orders['total'] += $rOrder->total;

			if($rOrder->status=2)	// Processed
				$orders['processed']++;
			elseif(($rOrder->status=3) || ($rOrder->status=3))	// Failed Or Declined
				$orders['failed']++;
			elseif($rOrder->status=1)	// Queued
				$orders['queued']++;
		}

		return $orders;
	}
	//======================================================================================================================================

	static function countOrdersSinceLastLogin() {
		$auth = VsAuth::Instance();
		$q = "SELECT orders.id, orders.status, order_totals.total FROM `orders`, `order_totals` WHERE ".$auth->lastLogin()."<=orders.date AND order_totals.orders_id=orders.id";


		$orders = array('processed'=>0, 'failed'=>0, 'queued'=>0, 'total'=>0);
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($rOrder = mysql_fetch_object($res)) {
			$orders['total'] += $rOrder->total;

			if($rOrder->status=2)	// Processed
				$orders['processed']++;
			elseif(($rOrder->status=3) || ($rOrder->status=3))	// Failed Or Declined
				$orders['failed']++;
			elseif($rOrder->status=1)	// Queued
				$orders['queued']++;
		}

		return $orders;
	}
	//======================================================================================================================================


	static function makeOrdersForToday() {
		$q = "SELECT id FROM `orders` WHERE DAY(CURDATE())=DAY(date)";

		$orders = array();
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($rOrder = mysql_fetch_object($res)) {
			array_push($orders, new VsOrder($rOrder->id));
		}

		return $orders;
	}
	//==================================================================================================================================

	static function makeOrdersForThisMonth() {
		$q = "SELECT id FROM `orders` WHERE MONTH(CURDATE())=MONTH(date)";

		$orders = array();
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($rOrder = mysql_fetch_object($res)) {
			array_push($orders, new VsOrder($rOrder->id));
		}

		return $orders;
	}
	//=====================================================================================================================================

	static function makeOrdersThisWeek() {
		$q = "SELECT id FROM `orders` WHERE DATE_SUB(CURDATE(),INTERVAL DAYOFWEEK(CURDATE()) DAY) <= date";

	}
	//==================================================================================================================================

	static function makeOrdersSinceLastLogin() {
		$auth = VsAuth::Instance();
		$q = "SELECT id FROM `orders` WHERE ".$auth->lastLogin()."<=date";


		
	}
	//==================================================================================================================================
};

?>
